//
//  YBManageWalletViewController.m
//  Nectar
//
//  Created by famer on 2023/11/8.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBManageWalletViewController+LayoutConstraints.h"
#import "YBNavigationBar.h"
#import "RevDataGetUserWallet.h"
#import "YBUserInfoManager.h"
#import "YBTostView.h"

@interface YBManageWalletViewController ()
@property (strong, nonatomic) RevDataGetUserWallet *walletInfo;
@end

@implementation YBManageWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kWhiteColor;
    
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.contentView];
    
    [self.contentView addSubview:self.userNameContentView];
    [self.userNameContentView addSubview:self.userNameLabel];
    [self.userNameContentView addSubview:self.userNameValueLabel];
    
    [self.contentView addSubview:self.walletContentView];
    [self.walletContentView addSubview:self.walletAddressLabel];
    [self.walletContentView addSubview:self.walletAddressValueLabel];
    [self.walletContentView addSubview:self.walletLineView];
    [self.walletContentView addSubview:self.walletNameLabel];
    [self.walletContentView addSubview:self.walletNameValueLabel];
    [self.walletContentView addSubview:self.walletNameEditImageView];
    [self.walletContentView addSubview:self.walletNameEditButton];
    
    [self setLayoutConstraints];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationBar resetBarItemState];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getUserWallet];
}

#pragma mark - Private Method
- (void)showModifyWalletNameAlter{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Wallet Name" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加一个输入框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Please enter";
    }];
    
    // 添加取消按钮
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    
    // 添加确认按钮
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UITextField *textField = alertController.textFields.firstObject;
        NSString *inputText = textField.text;
        // 在这里处理用户输入的数据
        NSLog(@"User input: %@", inputText);
        if([[NSObject nullSafeForObject:inputText] length]>0){
            [self updateWalletName:inputText];
        }
    }];
    [alertController addAction:confirmAction];
    
    // 显示警告框
    [self presentViewController:alertController animated:YES completion:nil];
    
}


- (void)getUserWallet{
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    [params setObject:self.coinType forKey:@"coinType"];
    NSString *urlString = [NSString stringWithFormat:@"%@",YB_URL_GET_WALLET];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        NSLog(@"getUserWallet: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            RevDataGetUserWallet *wallet = [RevDataGetUserWallet mj_objectWithKeyValues:obj.data];
            self.walletInfo = wallet;
            
            NSLog(@"valid name: %@", [wallet getValidName]);
            self.walletNameValueLabel.text = [wallet getValidName];
            self.walletAddressValueLabel.text = wallet.address;
            [self setLayoutConstraints];
        }
        
    } failure:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

- (void)updateWalletName:(NSString *)walletName{
    [SVProgressHUD show];
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    [params setObject:walletName forKey:@"name"];
    [params setObject:self.walletInfo.walletId forKey:@"walletId"];
    NSString *urlString = [NSString stringWithFormat:@"%@",YB_URL_SET_WALLET_NAME];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        NSLog(@"updateWalletName: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            NSLog(@"-----%@", obj.data);
            [YBTostView showWithTitle:@"Success"];
            [self getUserWallet];
        }
        
    } failure:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

#pragma mark - Getter Setter
YBViewGetter(contentView, _contentView, [UIColor hexColor:@"F4F5F7"])
YBViewGetter(walletContentView, _walletContentView, [UIColor hexColor:@"FFFFFF"])
YBViewGetter(userNameContentView, _userNameContentView, [UIColor hexColor:@"FFFFFF"])
YBViewGetter(walletLineView, _walletLineView, kThemeSplitLineColor)

YBLabelGetter(userNameLabel, _userNameLabel, [UIFont systemFontOfSize:14], @"Identity wallet user name", kThemeBlackColor, NSTextAlignmentLeft);
YBLabelGetter(userNameValueLabel, _userNameValueLabel, [UIFont systemFontOfSize:12], [YBUserInfoManager shareInstance].username, [UIColor hexColor:@"84828C"], NSTextAlignmentLeft);

YBLabelGetter(walletAddressLabel, _walletAddressLabel, [UIFont systemFontOfSize:14], @"Wallet Address", kThemeBlackColor, NSTextAlignmentLeft);
YBLabelGetter(walletAddressValueLabel, _walletAddressValueLabel, [UIFont systemFontOfSize:12], @"*", [UIColor hexColor:@"84828C"], NSTextAlignmentLeft);

YBLabelGetter(walletNameLabel, _walletNameLabel, [UIFont systemFontOfSize:14], @"Wallet Name", kThemeBlackColor, NSTextAlignmentLeft);
YBLabelGetter(walletNameValueLabel, _walletNameValueLabel, [UIFont systemFontOfSize:12], @"", [UIColor hexColor:@"84828C"], NSTextAlignmentLeft);

YBImageViewGetter(walletNameEditImageView, _walletNameEditImageView, [UIImage imageNamed:@"icon_right_black"])

- (UIButton *)walletNameEditButton{
    if(!_walletNameEditButton){
        _walletNameEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _walletNameEditButton.backgroundColor = kClearColor;
        [_walletNameEditButton setTitle:@"" forState:UIControlStateNormal];
        [_walletNameEditButton setTitle:@"" forState:UIControlStateSelected];
        [_walletNameEditButton addTarget:self action:@selector(showModifyWalletNameAlter) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _walletNameEditButton;
}

- (YBNavigationBar *)navigationBar{
    if(!_navigationBar){
        _navigationBar = [[YBNavigationBar alloc] initWithFrame:CGRectZero];
        _navigationBar.backgroundColor = kWhiteColor;
        [_navigationBar setTitle:@"Manage" titleColor:kThemeBlackColor];
        [_navigationBar setLeftBarImage:@"icon_nav_back" forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        _navigationBar.onLeftBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _navigationBar;
}
@end
